<?php /*
	
*/ ?>
<?php include("system/config.inc.php");
$donotinclude = 1;
if(!isset($_SESSION['adminuserid']))
{
	header("location:index.php?msg=logfirst");
	die();
}
// INSERT into database.
if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")
	{
				
				$cust=mysql_real_escape_string($_REQUEST['customer']);
				$code=mysql_real_escape_string($_REQUEST['disccode']);
				$start=mysql_real_escape_string($_REQUEST['startdate']);
				$expire = mysql_real_escape_string($_REQUEST['exipiredate']);
				$disc=mysql_real_escape_string($_REQUEST['discount']);
				if(isset($_REQUEST['radiobutton']) && ($_REQUEST['radiobutton'])==1)
				{
					$type=1;
				}
				else if(isset($_REQUEST['radiobutton']) && ($_REQUEST['radiobutton'])==2)
				{
					$type=2;
				}
				$amttime=mysql_real_escape_string($_REQUEST['amttime']);
				$min=mysql_real_escape_string($_REQUEST['minbuy']);
				$max=mysql_real_escape_string($_REQUEST['maxbuy']);
				//$time= date("D: H : i : s : A");
				$insert="INSERT INTO `tbldiscountcode` ( `intcusid` , `varcode` ,`dtstart` ,`dtexpire`,`intstatus`,`intamttime`,`decdiscount`,`intdesctype`,`decminbuy`,`decmaxbuy`) 
				VALUES ($cust,'$code', '$start','$expire',1,$amttime,'$disc',$type,'$min','$max')";
				$sql=$obj_db->insert($insert);
				
				if ($cust!=0) {
					$msg_subject="Your Discount Code";
					$mes="Congratulations you have been given a discount code for ".SITENAME."\n\nYour Code is (".$code.") please enter this code when proceeding through the checkout process";
					$message=$mes;
					$headers="From:".SITENAME;
					mail($cust,$msg_subject,$message,$headers);
				}
		header("location:discountcode.php?msg=add");
		die();
	}	

// UPDATE the record
$p_cust="";
$p_code="";
$p_expire = "";
$p_disc="";
$p_min="";
$p_max="";
$p_start="";
$p_amttme="";
$p_type="";
$action=1;
if((isset($_REQUEST['a']) && trim($_REQUEST['a'])==2) && (!(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")))
{
 	if(isset($_REQUEST['intid']) && trim($_REQUEST['intid'])!="")
 	{
		$id =  $_REQUEST['intid'];
		$sql = "select * from tbldiscountcode where intid ='$id'";
		$result = $obj_db->select($sql);
		if(count($result)<=0)
		{
			echo " No Record Found!<br>";
			die();
		}
		if($result)
		{
			foreach($result as $row)
			{
				
				$p_cust=$row['intcusid'];
				$p_code=$row['varcode'];
				$p_start=$row['dtstart'];
				$p_expire = $row['dtexpire'];
				$p_amttme=$row['intamttime'];
				$p_disc=$row['decdiscount'];
				$p_min=$row['decminbuy'];
				$p_max=$row['decmaxbuy'];
				$p_type=$row['intdesctype'];
				$action=2;
			}
		}
		if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Update")
		{
				$cust=$_REQUEST['customer'];
				$code=$_REQUEST['disccode'];
				$start=$_REQUEST['startdate'];
				$expire = $_REQUEST['exipiredate'];
				//echo $expire; die();
				$disc=$_REQUEST['discount'];
				if(isset($_REQUEST['radiobutton']) && ($_REQUEST['radiobutton'])==1)
				{
					$type=1;
				}
				else if(isset($_REQUEST['radiobutton']) && ($_REQUEST['radiobutton'])==2)
				{
					$type=2;
				}
				$amttime=$_REQUEST['amttime'];
				$min=$_REQUEST['minbuy'];
				$max=$_REQUEST['maxbuy'];
			
			$sql_upd ="UPDATE tbldiscountcode SET  
						intcusid = '$cust',
						varcode='$code',
						dtstart='$start',
						dtexpire='$expire',
						intamttime='$amttime',
						intdesctype='$type',
						decdiscount='$disc',
						decminbuy='$min',
						decmaxbuy='$max'
						 where intid ='$id'";
			$sqlupd = $obj_db->edit($sql_upd);
			//echo $sql_upd;
			//print_r($result);
			//die();
			$action=1;
			
			
	header("location:discountcode.php?msg=edit");
				die();	 		
}}

}
// DELETE record from database
if(isset($_REQUEST['a']) && trim($_REQUEST['a'])==3)
{
if(isset($_REQUEST['intid']) && trim($_REQUEST['intid'] != ""))
{
$id=$_REQUEST['intid'];
$sql_del = "Delete from `tbldiscountcode` WHERE intid ='$id'";
//echo $sql_del; die();
$del = mysql_query($sql_del);

header("location:discountcode.php?msg=del");
die();
}
}

?>
<script language="javascript" src="js/scw1.js"></script>
<script language="javascript" src="js/discountcode.js"></script>
<?php include("inc/header.php");?>
<body>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="1" class="middlebackground">
  <tr>
    <td colspan="3" valign="top" align="center"><?php include(INC."top.inc.php");?></td>
  </tr>
  <tr>
    <td width="2%">&nbsp;</td>
    <td width="95%"><?php   
   if(isset($_REQUEST['script']))
	{
	if(trim($_REQUEST['script'])=='adddiscount' || trim($_REQUEST['script'])=='editdiscount')
	{
	?>
		<form action="" method="post" enctype="multipart/form-data" name="discount">
		
      <table width="60%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
        <tr class="bg1">
          <td colspan="5" align="center"><strong>
            <?php if($_REQUEST['script']=='adddiscount'){echo "Add New Discount...";}
	if($_REQUEST['script']=='editdiscount'){echo "Edit Discount...";}?>
          </strong></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="left">&nbsp;</td>
          <td>&nbsp;</td>
          <td align="right">(* All fields are Required)</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="2%">&nbsp;</td>
          <td width="30%" align="right" class="sltimg"> *Customer </td>
          <td width="3%">:</td>
          <td width="63%" align="left"><label>
            <select name="customer" id="customer">
              <option value="0">All</option>
              <?php 
						$sql="select * from tblcustomers";
						$resman = $obj_db->select($sql);
						if(count($resman)>0)
						{
							for($m=0;$m<count($resman);$m++)
							{
								
							
					?>
              <option value="<?php echo $resman[$m]['intcusid'];?>"<?php if($resman[$m]['intcusid'] == $p_cust){echo "selected";}else{echo "";} ?>>
                <?php echo $resman[$m]['varcustemail'];?>
                </option>
              <?php 
							}
						}
					?>
            </select>
          </label></td>
          <td width="2%">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" class="sltimg">*Discount Code </td>
          <td>:</td>
          <td align="left"><input name="disccode" type="text" id="disccode" value="<?php echo $p_code;?>" maxlength="20" /></td>
          <td>&nbsp;</td>
        </tr>

        <tr>
          <td align="left">&nbsp;</td>
          <td align="right" class="sltimg">*Start Date </td>
          <td align="left">:</td>
          <td align="left"><input name="startdate" type="text" id="startdate" value="<?php echo $p_start;?>"  onmouseover="scwShow(this,this);"/></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td align="left">&nbsp;</td>
          <td align="right" class="sltimg">*Expire Date</td>
          <td align="left">:</td>
          <td align="left"><input name="exipiredate" type="text" id="exipiredate" value="<?php echo $p_expire;?>"  onmouseover="scwShow(this,this);"/></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td align="left">&nbsp;</td>
          <td align="right" class="sltimg">*Discount(percent/fix)</td>
          <td align="left">:</td>
          <td align="left"><input name="discount" type="text" id="discount" value="<?php echo $p_disc;?>" size="10" maxlength="2"  /></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td align="left">&nbsp;</td>
          <td align="right" class="sltimg">*Discount Type</td>
          <td align="left">:</td>
          <td align="left" class="sltimg"><input name="radiobutton" type="radio" value="1" <?php if($p_type==1){echo "checked";}?> />
            Percent
            <input name="radiobutton" type="radio" value="2" <?php if($p_type==2){echo "checked";}?>/> 
            Fix </td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td align="left">&nbsp;</td>
          <td align="right" class="sltimg">*Number of uses </td>
          <td align="left">:</td>
          <td align="left"><input name="amttime" type="text" id="amttime" value="<?php echo $p_amttme;?>" maxlength="20" /></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td align="left">&nbsp;</td>
          <td align="right" class="sltimg">*Minimum Buy  </td>
          <td align="left">:</td>
          <td align="left"><input name="minbuy" type="text" id="minbuy" value="<?php echo $p_min;?>" maxlength="20" /></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td align="left">&nbsp;</td>
          <td align="right" class="sltimg">*Maximum Buy </td>
          <td align="left">:</td>
          <td align="left"><input name="maxbuy" type="text" id="maxbuy" value="<?php echo $p_max;?>" maxlength="20" /></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="5" align="left">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="5" align="center"><input name="Submit" type="submit" class="btn" id="Submit" value="<?php echo ($action==2) ? "Update":"Submit"; ?>"  onclick="return check();"/></td>
        </tr>
        <tr>
          <td colspan="5" align="center">&nbsp;</td>
        </tr>
      </table>
    <?php
	}
}
else
{
?>

    <table width="90%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
      <tr class="bg1">
        <td colspan="8" align="left"><strong>Discount Code...</strong></td>
        <td align="center"><a href="discountcode.php?script=adddiscount" class="aa">[NEW]</a></td>
      </tr>
      <tr class="tblbg3">
        <td align="center"><strong>Customer</strong></td>
        <td align="center"><strong>Code</strong></td>
        <td align="center"><strong>Expire Date</strong></td>
        <td align="center"><strong>Discount</strong></td>
        <td align="center"><strong>Minimum Buy</strong></td>
		<td align="center"><strong>Maximum Buy</strong></td>
		<td align="center"><strong>Status</strong></td>
        <td align="center"><strong>Edit</strong></td>
        <td align="center"><strong>Delete</strong></td>
      </tr>
      <?php
	  
	  
	  // change status disable or active
if(isset($_REQUEST['status']))
{
	if(isset($_REQUEST['s']) && trim($_REQUEST['s'])==0)
	 {
		if(isset($_REQUEST['id']) && trim($_REQUEST['id']!=""))
		{
			$id=$_REQUEST['id'];
			$update = $obj_db->sql_query("update tbldiscountcode set intstatus = 1 where intid=$id");
			header("location:discountcode.php");
			die();
		}
	}
	if(isset($_REQUEST['s']) && trim($_REQUEST['s'])==1)
	{
		if(isset($_REQUEST['id']) && trim($_REQUEST['id']!=""))
		{
			$id=$_REQUEST['id'];
			$update = $obj_db->sql_query("update tbldiscountcode set intstatus = 0 where intid=$id");
			header("location:discountcode.php");
			die();
		}
	}
}
	  
	  /**************************************
		PAGING CODE START
		**************************************/
		//$rowperpage=23;
		$tablename="tbldiscountcode";
		$per_page_keywords= "";
		$per_page_sorts="";
		include("system/paging.inc.php");

		/**************************************
		PAGING CODE ENDING
		**************************************/
		?>
      <?php
   		$sql="select * from tbldiscountcode Limit ".($page_no*$row_per_page).",".$row_per_page;
		$result=$obj_db->select($sql);
  		if($result)
		{
   		for($i=0;$i<count($result);$i++)
   		{
            $id=$result[$i]['intid'];
			$type=$result[$i]['intdesctype'];
			
			?>
      <tr class="<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>" onMouseOver="this.className='Hrhover';"  onmouseout="this.className='<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>';">
        <td align="center">
		<?php 
		
		$custid= $result[$i]['intcusid'];
		if($custid==0)
		{
			echo "All";
		}
		else
		{
		$sql="select * from tblcustomers where intcusid='$custid'";
		$resman = $obj_db->select($sql);
		if(count($resman)>0)
		{
			for($m=0;$m<count($resman);$m++)
			{
				echo $resman[$m]['varcustfname'];
			}
								
		}
		}
		?>		</td>
		
		
        <td align="center"><?php echo $result[$i]['varcode'];?></td>
        <td align="center"><?php echo $result[$i]['dtexpire'];?></td>
        <td align="center"><?php echo $result[$i]['decdiscount'];?><?php if($type==1){echo "(Percent)";} else if($type==2){echo "(Flat)";}?></td>
		 <td align="center"><?php echo $result[$i]['decminbuy'];?></td>
		  <td align="center"><?php echo $result[$i]['decmaxbuy'];?></td>
        <td align="center"><table border="0" cellspacing="0" cellpadding="0" align="center">
				  <tr>
		<?php 
					if($result[$i]['intstatus']==0)
					{
						
						echo "<td><a class='aa' Title='Click here to Enable Discount Code' href='discountcode.php?status=0&s=0&id=$id'>Enable</a></td>";		
					}
					if($result[$i]['intstatus']==1)
					{
						
						echo "<td><a class='aa' Title='Click here to Disable Discount Code' href='discountcode.php?status=1&s=1&id=$id'>Disable</a></td>";
					}
				?>
				</tr></table></td>
        <td align="center"><a class="link" Title="Click here to Edit" href="discountcode.php?script=editdiscount&amp;a=2&amp;intid=<?php echo $result[$i]['intid']; ?>"><img src="images/edit.bmp" border="0"/></a></td>
        <td align="center"><a Title="Click here to Delete" href="discountcode.php?a=3&amp;intid=<?php echo $result[$i]['intid']; ?>" onClick="return confirm('Are you sure to delete this record?');"><img src="images/delete.bmp" border="0"/></a></td>
      </tr>
      <?php }}?>
      <tr>
        <td colspan="9" align="center"><?php
pagindet_atbotttom_page($div_page_no,$page_no,$req_querystr,$total_db_rec,$row_per_page);
?></td>
      </tr>
    </table>
    <?php }?>
	</form>
	</td>
    <td width="3%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><?php include("inc/bottom.php");?></td>
  </tr>
</table>


